package com.VolunteerPlatform.Controller.volunteer;

import com.VolunteerPlatform.Entity.VolunteerInfo;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by DoveXL on 2018/12/6.
 */
@Controller
@RequestMapping(value = "/PersonInfo/**")
public class PersonInfoController {
    @Autowired
    private VolunteerInfoService volunteerInfoService;

    @RequestMapping(value = "CertainVolunteerInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getAllVolunteerInfo(@RequestBody String jsondata, HttpSession httpSession)
    {
        int rowCount=0;
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        int page = jsonObject.getInteger("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String user_flag = jsonObject.getString("user_flag");
        String IDCard = jsonObject.getString("IDCard");
        String Gender = jsonObject.getString("Gender");
        String Name = jsonObject.getString("Name");
        String Unit = jsonObject.getString("Unit");
        String Phonenum = jsonObject.getString("Phonenum");
        JSONArray UserInfoArray = new JSONArray();
        List<VolunteerInfo> volunteerInfoList = volunteerInfoService.getVolunteerInfoService(IDCard,Name,Gender,Unit,Phonenum,0,-1,-1);
        try {
            rowCount = volunteerInfoList.size();
            if(rowCount%pagesize!=0)
            {
                rowCount = rowCount / pagesize + 1;
            }
            else
            {
                rowCount = rowCount / pagesize;
            }
            List<VolunteerInfo> VolunteerInfoList = volunteerInfoService.getVolunteerInfoService(IDCard,Name,Gender,Unit,Phonenum,Integer.parseInt(user_flag),(page-1)*pagesize,pagesize);
            System.out.println(VolunteerInfoList.size());

            for (int i=0;i<VolunteerInfoList.size();i++) {
                JSONObject UserInfo = new JSONObject();
                UserInfo.put("user_name", VolunteerInfoList.get(i).getName());
                UserInfo.put("user_gender", VolunteerInfoList.get(i).getGender());
                UserInfo.put("user_Unit", VolunteerInfoList.get(i).getUnit());
                UserInfo.put("user_IDCard", VolunteerInfoList.get(i).getIDCardNumber());
                UserInfo.put("user_phonenum", VolunteerInfoList.get(i).getPhonenum());
                UserInfoArray.add(UserInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelist",UserInfoArray);
        return JSON.toJSONString(jsonObject1);
    }


    @RequestMapping(value = "ExportPersonInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ExportPersonInfo(@RequestBody String jsondata,HttpSession httpSession) throws IOException {

        JSONObject jsonObject = JSON.parseObject(jsondata);
        String checkFieldName = jsonObject.getString("checkFieldName");
        String Name = jsonObject.getString("Name");
        String IDCardNumber = jsonObject.getString("IDCardNumber");
        String Unit = jsonObject.getString("Unit");
        String Gender = jsonObject.getString("Gender");
        List<String> checkFieldNameList = JSON.parseArray(checkFieldName,String.class);
        List<String> checkFieldChineseNameList = new ArrayList<>();
        Map<String,String> PersonInfoChineseMap = new HashMap<>();
        PersonInfoChineseMap.put("Number","序号");
        PersonInfoChineseMap.put("Unit","单位");
        PersonInfoChineseMap.put("Name","姓名");
        PersonInfoChineseMap.put("Gender","性别");
        PersonInfoChineseMap.put("Birthday","出生年月");
        PersonInfoChineseMap.put("Phonenum","联系方式");
        PersonInfoChineseMap.put("IDCardNumber","身份证号");
        PersonInfoChineseMap.put("BankAccount","银行账户");
        PersonInfoChineseMap.put("BankBranch","开户银行");
        String title = "志愿者个人信息(银行账户)一览表";
        String[] rowsName = new String[checkFieldNameList.size()];
        for(int i =0;i<checkFieldNameList.size();i++)
        {
            checkFieldChineseNameList.add(PersonInfoChineseMap.get(checkFieldNameList.get(i)));
        }
        checkFieldChineseNameList.toArray(rowsName);
        int flag =0;
        String filePath = "D:\\code\\IntelIJ IDEA\\GeoLife\\LongHuaVolunteer\\target\\com.ecust\\files\\download\\";
        String fileName = "Personinfo"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xls";
        flag =volunteerInfoService.ExportPersonInfoService(Name,IDCardNumber,Unit,Gender,title,rowsName,checkFieldNameList,filePath+fileName);
        httpSession.setAttribute("ExportPersonInfoName",fileName);
        return String.valueOf(flag);
    }


    @RequestMapping(value = "checkIDCardNumber", method = RequestMethod.POST)
    @ResponseBody
    public String checkIDCardNumberInfo(@RequestBody String IDCardNumber) throws IOException {

        JSONObject jsonObject = JSON.parseObject(IDCardNumber);
        String IDCardNumber1 = jsonObject.getString("IDCardNumber");
        List<VolunteerInfo> volunteerInfos = volunteerInfoService.getVolunteerInfoService(IDCardNumber1,null,null,null,null,0,-1,-1);
        System.out.println(volunteerInfos.size());
        if(volunteerInfos.size()==0)
        {
            return "1";
        }else
        {
            return "0";
        }
    }



    @RequestMapping(value = "AddVolunteerInfo", method = RequestMethod.POST)
    @ResponseBody
    public String AddVolunteerInfo(@RequestBody String jsondata) throws IOException {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String PersonName = jsonObject.getString("PersonName");
        String IDCardNumber = jsonObject.getString("IDCardNumber");
        String Unit = jsonObject.getString("Unit");
        String Phonenum = jsonObject.getString("Phonenum");
        String Gender = "男";
        String Birthday = "1994-05";
        if(IDCardNumber.length()==15)
        {
            String GenderNumber = IDCardNumber.substring(14);

            if(Integer.parseInt(GenderNumber)%2==0)
            {
                Gender = "女";
            }else
            {
                Gender = "男";
            }
            String BirthdayYear = IDCardNumber.substring(6,8);
            String BirthdayMonth = IDCardNumber.substring(8,10);
            Birthday = "19"+BirthdayYear+"-"+BirthdayMonth;
        }else if (IDCardNumber.length()==18)
        {
            String GenderNumber = IDCardNumber.substring(16,17);
            if(Integer.parseInt(GenderNumber)%2==0)
            {
                Gender = "女";
            }else
            {
                Gender = "男";
            }
            String BirthdayYear = IDCardNumber.substring(6,10);
            String BirthdayMonth = IDCardNumber.substring(10,12);
            Birthday = BirthdayYear+"-"+BirthdayMonth;

        }else
        {
            return "1";
        }
        int flag = volunteerInfoService.insertVolunteerInfoService(IDCardNumber,PersonName,Gender,Birthday,Unit,Phonenum,0);
        if(flag==1)
        {
            return "1";
        }else
        {
            return "0";
        }
    }



    @RequestMapping(value = "DeletePersonInfo", method = RequestMethod.POST)
    @ResponseBody
    public String DeletePersonInfo(@RequestBody String jsondata) throws IOException {

        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCardNumber1 = jsonObject.getString("IDCardNumber");
        int flag = volunteerInfoService.deleteVolunteerInfoService(IDCardNumber1,null,null,null,1);
        if(flag==1)
        {
            return "1";
        }else
        {
            return "0";
        }
    }



    @RequestMapping(value = "ChangeVolunteerInfo", method = RequestMethod.POST)
    @ResponseBody
    public int ChangeVolunteerInfo(@RequestBody String jsondata) {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String OriginIDCardNumber = jsonObject.getString("OriginIDCardNumber");
        String NewPersonName = jsonObject.getString("PersonName");
        String PersonName = jsonObject.getString("OriginPersonName");
        String NewIDCardNumber = jsonObject.getString("IDCardNumber");
        String NewUnit = jsonObject.getString("Unit");
        String NewPhonenum = jsonObject.getString("Phonenum");
        return volunteerInfoService.updateVolunteerInfoService(OriginIDCardNumber,NewIDCardNumber,NewPersonName,PersonName,NewUnit,NewPhonenum,-1);
    }





    @RequestMapping(value = "VolunteerAgeStatistics",method = RequestMethod.POST,produces ="application/json;charset=UTF-8" )
    @ResponseBody
    public String VolunteerAgeStatistics()
    {
        return JSON.toJSONString(volunteerInfoService.VolunteerAgeStatistics(null,null,null,null,null,0,-1,-1));

    }


    @RequestMapping(value = "VolunteerTeamNumberStatistics",method = RequestMethod.POST,produces ="application/json;charset=UTF-8" )
    @ResponseBody
    public String VolunteerTeamNumberStatistics()
    {
        return JSON.toJSONString(volunteerInfoService.VolunteerTeamNumberStatistics(null,null,null,null,null,0,-1,-1));
    }





















}
